package com.community.consumer.service.impl;

import com.community.common.entity.*;
import com.community.common.result.Result;
import com.community.consumer.entity.resp.GroupGoodResponse;
import com.community.consumer.entity.resp.GroupGoodSkuResoponse;
import com.community.consumer.entity.resp.GroupRecordListResponse;
import com.community.consumer.mapper.ConsumerMapper;
import com.community.consumer.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Autowired
    ConsumerMapper consumerMapper;

    /**
     * 查询可拼团的商品
     * @return
     */
    @Override
    public List<GroupGoodResponse> findGroupGoodList() {
        return consumerMapper.findGroupGoodList();
    }

    /**
     * 查找所有可拼团商品的sku
     * @return
     */
    @Override
    public List<GroupGoodSkuResoponse> findGroupSkuList() {
        return consumerMapper.findGroupSkuList();
    }

    /**
     * 添加订单
     * @param order
     * @return
     */
    @Override
    public int addOrder(Order order) {
        return consumerMapper.addOrder(order);
    }

    /**
     * 添加订单详情
     * @param orderMore
     * @return
     */
    @Override
    public int addOrderMore(OrderMore orderMore) {
        return consumerMapper.addOrderMore(orderMore);
    }

    /**
     * 查询订单总价
     * @param orderNum
     * @return
     */
    @Override
    public Double findOrderMoreAllPrice(String orderNum) {
        return consumerMapper.findOrderMorePrice(orderNum);
    }

    /**
     * 保存订单流水
     * @param orderSerial
     */
    @Override
    public void saveOrderSerial(OrderSerial orderSerial) {
        consumerMapper.saveOrderSerial(orderSerial);
    }

    /**
     * 查找订单流水
     * @param outTradeNo
     * @return
     */
    @Override
    public OrderSerial findOrderSerial(String outTradeNo) {
        return consumerMapper.findOrderSerial(outTradeNo);
    }

    /**
     * 修改订单流水
     * @param outTradeNo
     * @param i
     */
    @Override
    public void updateOrderSerial(String outTradeNo, int i) {
        consumerMapper.updaetOrderSerial(outTradeNo,i);

    }

    /**
     * 查找订单
     * @param orderNum
     * @return
     */
    @Override
    public Order selectOne(String orderNum) {
        return consumerMapper.findOrder(orderNum);
    }

    /**
     * 修改订单
     * @param order
     */
    @Override
    public void updateOrderSuccess(Order order) {
        consumerMapper.updateOrderSuccess(order);
    }

    /**
     * 查找拼团人数
     * @param groupId
     * @return
     */
    @Override
    public int findGroupRecordCount(Integer groupId) {
        return consumerMapper.findGroupRecordCount(groupId);
    }

    /**
     * 查找拼团记录
     * @param groupId
     * @return
     */
    @Override
    public Result<List<GroupRecordListResponse>> findGroupRecord(String groupId) {
        List<GroupRecordListResponse> listResponses = consumerMapper.findGroupRecord(groupId);
        return Result.success(listResponses);
    }

    /**
     * 查找用户订单
     * @param id
     * @param groupId
     * @return
     */
    @Override
    public Order findUserOrder(Integer id, Integer groupId,Integer flag) {
        return consumerMapper.findUserOrder(id,groupId,flag);
    }

    /**
     * 查找是否参加过此活动
     * @param id
     * @param groupId
     * @return
     */
    @Override
    public GroupRecord findIsJoinGroup(Integer id, String groupId) {
        return consumerMapper.findIsJoinGroup(id,groupId);
    }

    /**
     * 查找拼团商品
     * @param groupId
     * @return
     */
    @Override
    public GroupGoodResponse findGroupGoodOne(String groupId) {
         return consumerMapper.findGroupGoodOne(groupId);
    }

    /**
     * 修改拼团商品详情数量
     * @param orderNum
     */
    @Override
    public void updateGroupSkuNum(String orderNum) {
        // 需要修改的数据
        GroupGoodSku groupGoodSku = consumerMapper.findGroupSkuToUpdate(orderNum);
        // 修改数量
        consumerMapper.updateGroupSkuNum(groupGoodSku);
        // 查找成功数量
        int successCount = consumerMapper.findJoinGroupSuccessNumber(groupGoodSku.getGroupId());
        // 达到拼团总人数
        if(successCount == groupGoodSku.getVoliadNumber()){
            consumerMapper.updateGroupGoodStatus(groupGoodSku.getGroupId());
        }
    }

    /**
     * 添加拼团记录
     * @param groupRecord
     */
    @Override
    public int addOrderRecord(GroupRecord groupRecord) {
       return consumerMapper.addOrderRecord(groupRecord);
    }

    /**
     * 查找拼团状态
     * @param groupId
     * @return
     */
    @Override
    public int findGroupGood(Integer groupId) {
        return consumerMapper.findGroupGood(groupId);
    }

    /**
     * 查找是否创建过此商品的订单
     * @param order
     */
    @Override
    public int findGroupOrderSameUser(Order order) {
       return  consumerMapper.findGroupOrderSameUser(order);
    }

    /**
     * 修改拼团记录状态
     * @param order
     */
    @Override
    public int updateGroupStatus(Order order) {
       return consumerMapper.updateGroupRecordStatus(order);
    }
}
